//Requires Webpush.js (Sockets) & jquery.js (Ajax)

var ServerCallbacks = {}

//SOCKETS CALLBACKS
ServerCallbacks.SetPlayerId = function(response)
{
	var p = response['data']; //Player information
	
	var player = Bomberman.RegisterPlayer
	(
		new Player({"id":p.id,"position":{"x": p.position.x,"y": p.position.y}, "width": 50, "height": 50}),
		true
	);
	player.paint();
	
	Bomberman.onMove = function()
	{
		Action.UpdatePlayerPosition(Bomberman.currentPlayer);
	}
}

ServerCallbacks.InitializeOthersPlayers = function(response)
{
	var game = response['data'];
	
	for(var i in game.players)
	{
		if(game.players[i].id == Bomberman.currentPlayer.id) continue;
		
		var player = Bomberman.RegisterPlayer
		(
			new Player({"id":game.players[i].id,"position":{"x": game.players[i].position.x,"y": game.players[i].position.y}, "width": 50, "height": 50})
		);
		player.paint();
	}
}

ServerCallbacks.NewPlayerConnected = function(response)
{
	var p = response['data']; //Player information
	
	var player = Bomberman.RegisterPlayer
	(
		new Player({"id":p.id,"position":{"x": p.position.x,"y": p.position.y}, "width": 50, "height": 50}),
		false
	);
	player.paint();
}

ServerCallbacks.PlayerDisconnected = function(response)
{
	var p = response['data']; //Player information
	
	var result = Bomberman.UnregisterPlayer(p.id);
	Bomberman.Paint();
}

ServerCallbacks.UpdatePlayerPosition = function(response)
{
	var player = response['data'];
	
	//Update position
	Bomberman.setPlayerPosition(player.id, player.position.x, player.position.y);
	Bomberman.Paint();
}

ServerCallbacks.UpdatePlayersList = function(response)
{
	var players = response['data'];
	var html = "";
	
	for(var i in players)
	{
		html += players[i].nick+'<br>';
	}
	$("#users_list").html(html);
}
